/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.app.converter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.itgrail.pilot.app.command.cmo.AddKeyResultCmd;
import com.itgrail.pilot.app.command.cmo.DeleteKeyResultCmd;
import com.itgrail.pilot.app.command.cmo.UpdateKeyResultCmd;
import com.itgrail.pilot.app.command.co.KeyResultDetailCO;
import com.itgrail.pilot.app.command.co.UserCO;
import com.itgrail.pilot.domain.user.entity.KeyResultE;
import com.itgrail.pilot.domain.user.factory.KeyResultFactory;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.KeyResultDO;
import com.itgrail.pilot.infrastructure.tunnel.util.SessionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import com.itgrail.grail.convertor.ConverterI;
import com.itgrail.grail.utils.reflect.BeanMapper;

import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;

@Component
public class KeyResultClientConverter implements ConverterI {

    @Autowired
    private KeyResultFactory factory;

    public KeyResultE addClientToEntity(AddKeyResultCmd cmd) {
        KeyResultE keyResultE = factory.createKeyResultE();
        BeanUtils.copyProperties(cmd, keyResultE);
        if (SessionUtils.currentUserId() != null) {
            keyResultE.setCreated(SessionUtils.currentUserId());
        }
        return keyResultE;
    }

    public KeyResultE updateClientToEntity(UpdateKeyResultCmd cmd) {
        KeyResultE keyResultE = factory.createKeyResultE();
        BeanUtils.copyProperties(cmd, keyResultE);
        if (SessionUtils.currentUserId() != null) {
            keyResultE.setUpdated(SessionUtils.currentUserId());
            keyResultE.setUpdateTime(new Timestamp(new Date().getTime()));
        }
        keyResultE.setNotifyUserList(JSON.toJSONString(cmd.getNotifyUsers()));
        return keyResultE;
    }


    public KeyResultE deleteClientToEntity(DeleteKeyResultCmd cmd) {
        KeyResultE keyResultE = factory.createKeyResultE();
        BeanUtils.copyProperties(cmd, keyResultE);
        keyResultE.setDeleted(new Byte("1"));
        if (SessionUtils.currentUserId() != null) {
            keyResultE.setUpdated(SessionUtils.currentUserId());
        }
        return keyResultE;
    }

    public KeyResultDetailCO dataToClient(KeyResultDO keyResultDO) {
        KeyResultDetailCO map = BeanMapper.map(keyResultDO, KeyResultDetailCO.class);
        if (!StringUtils.isEmpty(keyResultDO.getNotifyUserList())) {
            map.setNotifyUsers(JSONArray.parseArray(keyResultDO.getNotifyUserList(), UserCO.class));
        } else {
            map.setNotifyUsers(new ArrayList<>());
        }
        return map;
    }

    public List<KeyResultDetailCO> dataToClients(List<KeyResultDO> list) {
        return Optional.ofNullable(list).orElse(Arrays.asList())
                .stream()
                .map(this::dataToClient)
                .collect(Collectors.toList());
    }
}
